<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>websocket</title>
</head>
<body>
    <script>
        // 获取到 websocket 实例
        const ws = new WebSocket("ws://localhost:3001");
        // 监听 open 事件(网络连接打开的时候向服务端发送数据)
        ws.addEventListener('open', function(e) {
            console.log("ws open");
            // 发送数据
            ws.send("哈哈");
        }, false);
        // 监听 message 事件
        ws.addEventListener('message', function(e) {
            // 拿到返回数据
            console.log("fd.", e.data);
        }, false);
    </script>
</body>
</html>